<%= simple_form_for(@article) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :title %>
  </div>

  <div class="form-inputs">
    <%= f.input :content %>
  </div>

  <%= f.hidden_field :user_id, value: current_user.id %>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

<dic id="image"></dic>


<%= file_field_tag 'image', id: 'upload' %>

<script type="text/javascript">

  $('#upload').change(function(){
    // var file = $(#upload).val();
    var data = new FormData();
    $.each($('#upload')[0].files, function(i, file) {
      data.append('file', file);
    });
    console.log($('#upload')[0].files)
    $.ajax({
       type: 'post',
       url: '/uploads.json',
       processData: false,
       contentType: false,
       data: data,
       dataType: 'json',
       success: function(response) {
         var html = "<input value=" + response['path'] + " type='hidden' name=[article][images_attributes][0][image] />";
         console.log(html)
         $("#article_user_id").append(html)
         var image = "<img src=\"" + response['path'] + "\"/>"
         console.log(image)
         $("#image").append(image)
       },
     });
  })

</script>
